# Maintainer: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: Eric Bélanger <eric@archlinux.org>

_name=feedparser
pkgname=python-feedparser
pkgver=6.0.10
pkgrel=3
pkgdesc='Parse RSS and Atom feeds in Python'
arch=('any')
url=https://github.com/kurtmckee/feedparser
license=('custom')
depends=('libxml2' 'python-sgmllib3k')
makedepends=('python-build' 'python-installer' 'python-setuptools'
             'python-wheel')
checkdepends=('python-chardet')
optdepends=('python-chardet: for character encoding auto-detection'
            'python-cchardet: for faster character encoding auto-detection')
source=("https://files.pythonhosted.org/packages/source/${_name::1}/$_name/$_name-$pkgver.tar.gz"
        'feedparser-fix-tests-on-py310.patch::https://github.com/kurtmckee/feedparser/commit/c55bd8ad37db89bd219783bc514d600c9523ed38.patch')
sha256sums=('27da485f4637ce7163cdeab13a80312b93b7d0c1b775bef4a47629a3110bca51'
            'df3ff5c144a9edabaee77fcf7aa465cc2a1a3ba85316830b7346f884367d155f')
b2sums=('044070eff7b3ef9422b1d4fb5a30e473feba55431d9fd2619573ac2f0f8c9e9e90e81f54797cbdaf965ca67b3f0cf169f478b7f08a933457df05e266238e6462'
        'b803c355e8ed8ee941e4478d3e05271eaf49686a45d32d1d62217811ea64e6315ff9d8e83456267e67ceb5f84c6a3000b61f5a7eccd3ead6fb8e2286864e651e')

prepare() {
  cd $_name-$pkgver
  patch --forward --strip=1 --input=../feedparser-fix-tests-on-py310.patch
}

build() {
  cd $_name-$pkgver
  python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd $_name-$pkgver
  # https://github.com/kurtmckee/feedparser/issues/236
  mv tests/illformed/undeclared_namespace.xml{,.bak}
  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  test-env/bin/python tests/runtests.py
  mv tests/illformed/undeclared_namespace.xml{.bak,}
}

package() {
  cd $_name-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl

  # Symlink license file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir"/usr/share/licenses/$pkgname
  ln -s "$site_packages"/$_name-$pkgver.dist-info/LICENSE \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
